package cn.com.controller.manageController;


import cn.com.Enum.SessionAttr;
import cn.com.Enum.UserAuth;
import cn.com.annotation.Auth;
import cn.com.constant.Code;
import cn.com.db.model.Admin;
import cn.com.db.model.Seller;
import cn.com.db.service.AdminService;
import cn.com.util.*;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


@Auth(UserAuth.ADMIN)
@RestController
@RequestMapping("/back/admin")
public class AdminManageController {
    @Autowired
    AdminService adminService;

    @Auth(UserAuth.ALL)
    @RequestMapping(value = "/login",method = RequestMethod.POST)
    public void login(HttpServletRequest request, HttpServletResponse response,
                      @RequestParam("authingId")String authingId)
    {
        DataResult dataResult=new DataResult();
        Admin admin=adminService.getAdminByAuthingId(authingId);
        if (admin==null){
            dataResult.setCode(Code.UN_AUTHORIZED);
            dataResult.setMessage("登录失败,管理员不存在");
            util.comeBack(response, JSON.toJSONString(dataResult));
            return;
        }
        SessionUtil.addAttr(request, SessionAttr.ADMINID,admin.getId());
        SessionUtil.addAttr(request,SessionAttr.ADMINAUTHINGID,admin.getAuthing_id());
        dataResult.setCode(Code.SUCCESS);
        dataResult.setMessage("登录成功");
        dataResult.setData(admin);
        util.comeBack(response,JSON.toJSONString(dataResult));
    }


}
